package com.sun.showcase.controller.mall;

import com.sun.showcase.biz.mall.service.GoodsService;
import com.sun.showcase.biz.mall.service.ShopService;
import com.sun.showcase.client.domain.mall.Goods;
import com.sun.showcase.client.query.mall.GoodsQuery;
import com.sun.showcase.client.query.mall.ShopQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Result;
import com.sun.showcase.utils.LoginContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value="/mall/goods")
public class GoodsController{
	
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private ShopService shopService;
	
	private Result result = new Result();
	/**
	 * 跳转到Goods管理页面
	 * 
	 * @return
	 */
	@RequestMapping(value="goGoods")
	public String goGoods(ModelMap map) {
		ShopQuery shopQuery = new ShopQuery();
		shopQuery.setUserId(LoginContextHolder.get().getUserId());
		List<ShopQuery> list = shopService.listAll(shopQuery);
		if(list.size() == 1) {
			map.put("shopId", list.get(0).getId());
			map.put("shopName", list.get(0).getName());
		}
		return "mall/goods";
	}
	/**
	 * 跳转到Goods新增页面
	 * 
	 * @return
	 */
	@RequestMapping(value="handleGoods")
	public String handleGoods(@RequestParam(value="id",required=false) Long id , ModelMap map) {
		return "mall/goods_rowEdit";
	}

	/**
	 * 跳转到查看desc页面
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="showDesc")
	public Goods showDesc(GoodsQuery goodsQuery) {
		return goodsService.get(goodsQuery);
	}

	/**
	 * 获得分页数据表格
	 */
	@ResponseBody
	@RequestMapping(value = "/datagrid")
	public DataGrid datagrid(GoodsQuery goodsQuery) {
		return goodsService.datagrid(goodsQuery);
	}
	

	/**
	 * 添加一个Goods
	 */
	@ResponseBody
	@RequestMapping(value="add")
	public Result add(@RequestBody GoodsQuery goodsQuery) {
		goodsService.add(goodsQuery);
		result.setSuccess(true);
		result.setMsg("添加成功！");
		return result;
	}

	/**
	 * 编辑Goods
	 */
	@ResponseBody
	@RequestMapping(value="edit")
	public Result edit(@RequestBody GoodsQuery goodsQuery) {
		goodsService.update(goodsQuery);
		result.setSuccess(true);
		result.setMsg("编辑成功！");
		return result;
	}

	/**
	 * 物理删除Goods
	 */
	@ResponseBody
	@RequestMapping(value="delete")
	public Result delete(GoodsQuery goodsQuery) {
		goodsService.delete(goodsQuery.getIds());
		result.setSuccess(true);
		result.setMsg("删除成功！");
		return result;
	}
	
	/**
	 * 逻辑删除Goods
	 */
	@ResponseBody
	@RequestMapping(value="deletePt")
	public Result deletePt(GoodsQuery goodsQuery) {
		goodsService.deletePt(goodsQuery.getIds());
		result.setSuccess(true);
		result.setMsg("删除成功！");
		return result;
	}
	/**
	 * 全部数据Goods
	 */
	@ResponseBody
	@RequestMapping(value="listAll")
	public Result listAll(GoodsQuery goodsQuery) {
		List<GoodsQuery> list = goodsService.listAll(goodsQuery);
		result.setSuccess(true);
		result.setMsg("成功！");
		result.setData(list);
		return result;
	}
}
